package com.yulebaby.callsenter.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.CallLogBean;
import com.yulebaby.callsenter.global.App;
import com.yulebaby.callsenter.utils.CommonUtil;
import com.yulebaby.callsenter.utils.DateUtil;
import com.yulebaby.callsenter.utils.ImageLoader;
import com.yulebaby.callsenter.utils.MediaPlayerUtils;
import com.yulebaby.callsenter.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 退出对话框
 */
public class CallFinishDialog extends Dialog implements View.OnClickListener, MediaPlayerUtils.CompletionListener {


    @BindView(R.id.iv_play)
    ImageView mIvPlay;
    @BindView(R.id.tv_phonename)
    TextView mTvPhonename;
    @BindView(R.id.tv_phonenum)
    TextView mTvPhonenum;
    @BindView(R.id.tv_type)
    TextView mTvType;
    @BindView(R.id.tv_calltime)
    TextView mTvCalltime;
    @BindView(R.id.tv_date)
    TextView mTvDate;

    private DialogCallBack callback;
    private Context context;
    private View mView;
    private Unbinder mUnbinder;
    private CallLogBean mCallLogBean;
    private MediaPlayerUtils mPlayerUtils;


    public CallFinishDialog(Context context, CallLogBean bean, DialogCallBack callback) {
        super(context, R.style.PickerDialogStyle);
        this.context = context;
        this.callback = callback;
        mCallLogBean = bean;
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mUnbinder.unbind();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = LayoutInflater.from(context).inflate(R.layout.dialog_call_finish, null);
        mUnbinder = ButterKnife.bind(this, mView);
        mPlayerUtils = MediaPlayerUtils.getInstances();
        mPlayerUtils.setOnCompletionListener(this);
        mTvPhonenum.setText(mCallLogBean.getCustomer_phone());
        mTvDate.setText(DateUtil.formatDateTimeDetail(mCallLogBean.getCall_time()));
        mTvCalltime.setText(DateUtil.timeChange(mCallLogBean.getCall_duration()).equals("0分0秒") ? "未接通" : DateUtil.timeChange(mCallLogBean.getCall_duration()));
        mTvPhonename.setText(mCallLogBean.getCustomer_name());


        switch (mCallLogBean.getPlayState()) {
            case 0:
                ImageLoader.load(App.mContext, R.drawable.phone_fail, mIvPlay);
                break;
            case 1:
                ImageLoader.load(App.mContext, R.drawable.phone_pause, mIvPlay);
                break;
            case 2:
                ImageLoader.load(App.mContext, R.drawable.phone_success, mIvPlay);
                break;
        }
        switch (mCallLogBean.getCall_type()) {
            case 1:
                mTvType.setText("呼入");
                break;
            case 2:
                mTvType.setText("呼出");
                break;
            case 3:
                mTvType.setText("未接通");
                break;
        }

        setContentView(mView);
        Window window = getWindow();
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = metrics.widthPixels;
        layoutParams.height = CommonUtil.getDimens(R.dimen.pick_dialog_heigh);
        window.setAttributes(layoutParams);

    }

    @OnClick({R.id.iv_close, R.id.iv_play, R.id.tv_addlater, R.id.tv_addsoon})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.iv_play:
//                MediaPlayerUtils.getInstances().
                switch (mCallLogBean.getPlayState()) {
                    case 0:
                        ToastUtil.showToast("没有录音文件");
                        break;
                    case 1:
                        mPlayerUtils.pause();
                        mCallLogBean.setPlayState(2);
                        ImageLoader.load(App.mContext, R.drawable.phone_success, mIvPlay);
                        break;
                    case 2:
                        mCallLogBean.setPlayState(1);
                        mPlayerUtils.openAudio(mCallLogBean.getFile_path());
                        ImageLoader.load(App.mContext, R.drawable.phone_pause, mIvPlay);
                        break;
                }
                break;
            case R.id.tv_addlater:
                dismiss();
                break;
            case R.id.tv_addsoon:
                if (callback != null) {
                    callback.onConfirm();
                }
                break;
        }
    }

    @Override
    public void Completion() {
        mCallLogBean.setPlayState(2);
        ImageLoader.load(App.mContext, R.drawable.phone_success, mIvPlay);
    }

    public interface DialogCallBack {
        void onConfirm();
    }
}
